package net.oschina.app.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spannable;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import net.oschina.app.MApp;
import net.oschina.app.R;
import net.oschina.app.bean.Active;
import net.oschina.app.emoji.InputHelper;
import net.oschina.app.ui.view.CircleImageView;
import net.oschina.app.ui.widget.MyURLSpan;
import net.oschina.app.util.PreferencesUtils;

import java.util.List;

/**
 * Created by huang on 2017-4-2.
 * 好友圈适配器
 */

public class FriendCircleAdapter extends BaseRecyclerViewAdapter{
    private List<Active> datas;
    public FriendCircleAdapter(Context context,List<Active> datas) {
        super(context);
        this.datas = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(MApp.mContext).inflate(R.layout.friend_circle_rv_item, parent,false);
        FriendCircleViewHolder holder = new FriendCircleViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        FriendCircleViewHolder viewHolder = (FriendCircleViewHolder) holder;
        Active active = datas.get(position);
        //初始化条目点击的状态
        initItemState(viewHolder, active);

        //设置item控件的值
        Glide.with(MApp.mContext).load(active.getPortrait()).fitCenter().into(viewHolder.cimgPortrait);//加载图片
        viewHolder.tvAuthor.setText(active.getAuthor());//作者
        viewHolder.tvPubData.setText(active.getPubDate());//发布时间

        //标题
        String objectTitle = active.getObjectTitle();
        if(!TextUtils.isEmpty(objectTitle)) {//标题不为空
            viewHolder.tvObjectTitle.setText(objectTitle);
            viewHolder.tvObjectTitle.setVisibility(View.VISIBLE);
        } else {
            viewHolder.tvObjectTitle.setVisibility(View.GONE);
        }

        //内容
        String message = active.getMessage();
        Spanned span = Html.fromHtml(message);
        if(TextUtils.isEmpty(message)) {//如果内容为空
            viewHolder.tvMessage.setVisibility(View.GONE);
        } else {
            viewHolder.tvMessage.setVisibility(View.VISIBLE);
            Spanned spanned = Html.fromHtml(message);
            viewHolder.tvMessage.setText(spanned);//富文本
            viewHolder.tvMessage.setMovementMethod(LinkMovementMethod.getInstance());//支持超链接
            viewHolder.tvMessage.setLongClickable(false);
            viewHolder.tvMessage.setFocusable(false);
        }
        Spannable spannable = InputHelper.displayEmoji(MApp.mContext.getResources(), span);
        viewHolder.tvMessage.setText(spannable);
        MyURLSpan.parseLinkText(viewHolder.tvMessage,spannable);

        //回复的数量
        if(active.getCommentCount() != 0) {//回复的数量不为0
            viewHolder.tvCommentCount.setVisibility(View.VISIBLE);
            viewHolder.tvCommentCount.setText(String.valueOf(active.getCommentCount()));
        } else {
            viewHolder.tvCommentCount.setVisibility(View.GONE);
        }
    }

    //初始化条目点击状态
    private void initItemState(FriendCircleViewHolder viewHolder, Active active) {
        String id = String.valueOf(active.getId());
        //获取已点击条目状态
        String ids = PreferencesUtils.getString(MApp.mContext, "fcItem_isRead","");//获取保存的数据
        if (ids.contains(id)) {//保存的数据包含条目数据的id
            //改变控件颜色
            viewHolder.tvObjectTitle.setTextColor(Color.parseColor("#ADAAAD"));
            viewHolder.tvMessage.setTextColor(Color.parseColor("#ADAAAD"));
        }else{
            //还原默认的控件颜色,解决条目复用样式还在的问题
            viewHolder.tvObjectTitle.setTextColor(Color.parseColor("#31719C"));
            viewHolder.tvMessage.setTextColor(Color.parseColor("#4A4D4A"));
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class FriendCircleViewHolder extends RecyclerView.ViewHolder{
        CircleImageView cimgPortrait;
        TextView tvAuthor;
        TextView tvPubData;
        TextView tvObjectTitle;
        TextView tvMessage;
        TextView tvCommentCount;
        public FriendCircleViewHolder(View itemView) {
            super(itemView);
            cimgPortrait = (CircleImageView) itemView.findViewById(R.id.cimg_firend_cricle_portrait);
            tvAuthor = (TextView) itemView.findViewById(R.id.tv_friend_circle_author);
            tvPubData = (TextView) itemView.findViewById(R.id.tv_friend_circle_pubData);
            tvObjectTitle = (TextView) itemView.findViewById(R.id.tv_friend_circle_objecttitle);
            tvMessage = (TextView) itemView.findViewById(R.id.tv_friend_circle_message);
            tvCommentCount = (TextView) itemView.findViewById(R.id.tv_friend_circle_commentCount);

        }
    }
}
